package com.zirenx.sys.service;

import com.jfinal.plugin.activerecord.Db;
import com.zirenx.sys.model.SysUserRole;

import java.util.List;

public class SysUserRoleService {

    SysUserRole dao = new SysUserRole().dao();

    /**
     * 根据用户id,获取roleId
     * @param userId
     * @return
     */
    public List getRoleIds(Long userId){
        String sql = "select t.role_id from "+dao.getTableName()+" t where t.user_id = ?";
        return Db.query(sql, userId);
    }

    /**
     * 批量保存
     * @param userId
     * @param roleIds
     */
    public void batchSave(String userId,String[] roleIds){
        if (roleIds!=null && roleIds.length>0){
            for (String roleId : roleIds){
                SysUserRole bean = new SysUserRole();
                bean.setRoleId(Long.valueOf(roleId));
                bean.setUserId(Long.valueOf(userId));
                bean.save();
            }
        }
    }

    /**
     * 批量删除
     * @param userId
     */
    public void batchRemove(String userId){
        Db.delete(" delete from "+dao.getTableName() +" where user_id = ?",userId);
    }
}
